package hotel.action;

import hotel.model.IRoom;
import hotel.service.RoomService;
import hotel.service.ServiceManager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 * This class get the list of rooms from DataBase
 * @author Alex
 *
 */
public class RoomListAction extends Action
{	
	private static final Logger logger = Logger.getLogger(RoomListAction.class);
	private static final String ROOMLISTACTION_ERROR = "Error in RoomListAction:";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
    	String mapper = null;
    	try
    	{
	    	RoomService roomService = ServiceManager.getDefault().getRoomService();
	        List<IRoom> rooms = null;
	        ActionMessages errors = new ActionMessages();
			
	        rooms = roomService.getRooms();
	        if (rooms.isEmpty() == true)
			{
				errors.add("norooms", new ActionMessage("error.norooms"));
	   	 	 	saveErrors(request,errors);
			}
	        request.setAttribute("rooms", rooms);
	        mapper = ForwardActionTo.SUCCESS; 
	    }
		catch(NullPointerException e)
		{
			logger.error(ROOMLISTACTION_ERROR + e);
		}
		return mapping.findForward(mapper);
    }
}
